<?php

/**
 * Merging a Contact's information
 * @package ModuleGestionDocuments
 * @version 1.0b
 * @var $_GET['model'] The model's ID
 * @var $_GET['contact'] The contact's ID
 * @return merged .odt
 */

session_start();

// Configuration file
include '../configuration.php';
// Functions
include '../inc/functions.php';

// Checking if user is logged in
if (!CheckSession()):
	exit();
endif;

// Include all objects
// TODO: Include only needed classes
foreach (glob($configuration['webroot'].'objects/*.php') as $class) { include_once $class; }

// odtPHP library
include '../odt/odf.php';

// Loading model
$model = new Model();
$model->Get($_GET['model']);
$file = $configuration['webroot'].'data/models/'.$model->modelId.'.'.$model->extension;
$odf = new odf($file);

// Loading contact
$contact = new Contact();
$contact->Get($_GET['contact']);
$date = '';
if (strlen($contact->birthdate) > 0):
	$date = explode('-', $contact->birthdate);
	$date = $date[2].'/'.$date[1].'/'.$date[0];
endif;

// Merging
try { $odf->setVars('contact_salutation', $contact->salutation); }
catch (Exception $e) {}
try { $odf->setVars('contact_lastname', $contact->lastname); }
catch (Exception $e) {}
try { $odf->setVars('contact_firstname', $contact->firstname); }
catch (Exception $e) {}
try { $odf->setVars('contact_birthdate', $date); }
catch (Exception $e) {}
try { $odf->setVars('contact_jobtitle', $contact->jobtitle); }
catch (Exception $e) {}
try { $odf->setVars('contact_email', $contact->email); }
catch (Exception $e) {}

// Exporting merged .odt for downloading
$odf->exportAsAttachedFile();
?>
